package com.adream.scalpel.app.client.ui;

import com.adream.scalpel.app.client.service.UserState;
import com.adream.scalpel.app.client.service.UserStateAsync;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;

public class TopPanel extends Composite {

    private Hyperlink logoutHyperlink;
    private Label userNameLabel;
    private Label companyNameLabel;
    private FlexTable flexTable;

    public TopPanel() {
        getUsername();
        flexTable = new FlexTable();
        initWidget(flexTable);
        flexTable.setWidth("100%");

        companyNameLabel = new Label("Company Name");
        flexTable.setWidget(0, 0, companyNameLabel);
        flexTable.getCellFormatter().setWidth(0, 0, "65%");
        companyNameLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
        companyNameLabel.setDirection(Direction.DEFAULT);
        flexTable.getCellFormatter().setWidth(0, 0, "15%");

        userNameLabel = new Label("Logged in as ");
        flexTable.setWidget(0, 1, userNameLabel);
        userNameLabel.setDirection(Direction.DEFAULT);
        flexTable.getCellFormatter().setWidth(0, 1, "30%");
        userNameLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

        logoutHyperlink = new Hyperlink("Logout", "Logout from scalpel");
        flexTable.setWidget(0, 2, logoutHyperlink);
        logoutHyperlink.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                onClickLogoutHyperlink(event);
            }
        });
        flexTable.getCellFormatter().setWidth(0, 2, "5%");
        flexTable.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
    }

    private void getUsername() {
        UserStateAsync userStateAsync = UserState.Util.getInstance();
        userStateAsync.getUserName(new AsyncCallback<String>() {

            public void onFailure(Throwable caught) {
            }

            public void onSuccess(String result) {
                userNameLabel.setText(userNameLabel.getText() + result);
            }
        });
    }

    protected void onClickLogoutHyperlink(final ClickEvent event) {
        RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST,
                "http://localhost:8080/Scalpel/j_spring_security_logout");
        requestBuilder.setCallback(new RequestCallback() {

            public void onError(Request request, Throwable exception) {
                Window.alert("Error:  " + exception.getMessage());
            }

            public void onResponseReceived(Request request, Response response) {
                String responseText = response.getText();
                if (responseText.indexOf("error:") >= 0) {
                    Window.alert("Could not log on. Invalid user or incorrect password!");
                } else if (responseText.indexOf("url:") >= 0) {
                    String url = responseText.replaceFirst("url:", "");
                    String host = Location.getHost();
                    String protocol = Location.getProtocol();
                    Location.assign(protocol + "//" + host + url);
                } else {
                    Window.alert("Well the server seems to have gone crazy! Try again later.");
                }
            }
        });
        try {
            requestBuilder.send();
        } catch (RequestException e) {
            e.printStackTrace();
        }
    }
}
